package cn.tedu.lianxi1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Serve1 {
    public static void main(String[] args) throws IOException {
        //开启服务器--端口号0~65535
        //在指定的8000的端口处，等待客户端的连接
        ServerSocket server=new ServerSocket(8000);
        System.out.println("服务器已开启.....");
        //等待客户端连接
        Socket socket = server.accept();
        System.out.println("接受一个客户端的请求...." );
        //开始通信
        //读取客户端发来的数据
        InputStream in2 = socket.getInputStream();
        BufferedInputStream in=new BufferedInputStream(in2);
        OutputStream out1 = socket.getOutputStream();
        BufferedOutputStream out=new BufferedOutputStream(out1);
        //读到的数据默认是int，转换成字符类型
        System.out.println("接收客户端的数据为：");
        PrintWriter writer=new PrintWriter(new PrintWriter(out));
        BufferedReader in1=new BufferedReader(new InputStreamReader(in2));
        char b;
        String line="";
        b = (char) in.read();
        writer.println(b);
        while (line!=null) {
            line= in1.readLine();
            System.out.println(line);
        }
        System.out.println();
        System.out.println("请输入发送给客户端数据：");
        String input=new Scanner(System.in).nextLine();
        out.write(input.getBytes());//String->[]
        out.flush();

    }
}
